package com.teach.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import org.junit.Test;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class JSONTest {

  @Test
  public void object2String(){
    User user1 = new User("张三", 18, true, new Date());
    User user2 = new User("李四", 28, true, new Date(0));
    User user3 = new User("王五", 19, true, new Date());
    User[] us = {user1,user2,user3};
    List<User> userList = Arrays.asList(us);
    System.out.println(user1);
    System.out.println(us);
    System.out.println(userList);
    System.out.println("-------------------------");
    System.out.println(JSON.toJSONString(user1));
    System.out.println(JSON.toJSONString(us));
    System.out.println(JSON.toJSONString(userList));
    System.out.println("-------------------------");
    System.out.println(JSON.toJSONString(user1,true));
    System.out.println(JSON.toJSONString(us,true));
    System.out.println(JSON.toJSONString(userList,true));
    System.out.println("-------------------------");
  }

  @Test
  public void string2Object(){
    String user1Str = "{\"brithdate\":\"2022-05-09\",\"sex\":true,\"userName\":\"张三\"}";
    String userListStr = "[{\"brithdate\":\"2022-05-09\",\"sex\":true,\"userName\":\"张三\"},{\"brithdate\":\"1970-01-01\",\"sex\":true,\"userName\":\"李四\"},{\"brithdate\":\"2022-05-09\",\"sex\":true,\"userName\":\"王五\"}]";
    System.out.println(user1Str);
    System.out.println(userListStr);

    User user = JSON.parseObject(user1Str,User.class);
    System.out.println(user);
    List<User> userList = JSON.parseArray(userListStr,User.class);
    System.out.println(userList);
    System.out.println("--------------------JSONObjecty");
    JSONObject obj= JSON.parseObject(user1Str);
    System.out.println("userName="+obj.getString("userName"));
    System.out.println("brithdate="+obj.getString("brithdate"));
    System.out.println("sex="+obj.getBoolean("sex"));
    System.out.println("-------------");
    for(String name : obj.keySet()) {
      System.out.println(name+"="+obj.get(name));
    }

    System.out.println("--------------------JSONArray");
    JSONArray arr = JSON.parseArray(userListStr);
    for(int i=0;i<arr.size();i++){
      JSONObject item = arr.getJSONObject(i);
      System.out.println("userName="+item.getString("userName"));
    }

  }

  public static class User {

    @JSONField(name = "userName")
    private String name;
    @JSONField(serialize = false,deserialize = false)
    private Integer age;
    private Boolean sex;
    @JSONField(format = "yyyy-MM-dd")
    private Date brithdate;

    public User() {
    }

    public User(String name, Integer age, Boolean sex, Date brithdate) {
      this.name = name;
      this.age = age;
      this.sex = sex;
      this.brithdate = brithdate;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public Integer getAge() {
      return age;
    }

    public void setAge(Integer age) {
      this.age = age;
    }

    public Boolean getSex() {
      return sex;
    }

    public void setSex(Boolean sex) {
      this.sex = sex;
    }

    public Date getBrithdate() {
      return brithdate;
    }

    public void setBrithdate(Date brithdate) {
      this.brithdate = brithdate;
    }

    @Override
    public String toString() {
      return "User{" +
          "name='" + name + '\'' +
          ", age=" + age +
          ", sex=" + sex +
          ", brithdate=" + brithdate +
          '}';
    }
  }
}
